package main;

/*
 * author by Xu
 */
//package com.bgate.framework.tools;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author chien
 */
public class PiPoDesigner {

    public static final int COLOR_BLACK = 0xFF000000;
    public static final int COLOR_YELOW = 0xFFFFFF00;
    public static final int COLOR_PINK = 0xFFFF7E00;
    public static final int COLOR_RED = 0xFFFF0000;
    public static final int COLOR_GREEN = 0xFF00FF00;
    public static final int COLOR_BLUE = 0xFF0000FF;
    public static final int COLOR_WHITE = 0xFFFFFFFF;
    private static byte PiPoRGB[] = null;
    private final static char[] arrChar = {
        'a', 'Ấ', 'ể', 'l', 'ơ', 'Ú', '2', ':', 'ữ', 'á', 'Ầ', 'Ê', 'L', 'ớ', 'Ù', '3', ';', 'Ữ', 'à', 'Ậ', 'Ế', 'm', 'ờ', 'Ụ', '4', '{', 'õ', 'ạ', 'Ẩ', 'Ề', 'M', 'ợ', 'Ủ', '5', '}', 'Õ', 'ả', 'b', 'Ệ', 'n', 'ở', 'ư', '6', '[', 'ỗ', 'A', 'B', 'Ể', 'N', 'Ơ', 'ứ', '7', ']', 'Ỗ', 'Á', 'c', 'f', 'o', 'Ớ', 'ừ', '8', '<', 'ỡ', 'À', 'C', 'F', 'ó', 'Ờ', 'ự', '9', '>', 'Ỡ', 'Ạ', 'd', 'g', 'ò', 'Ợ', 'ử', '!', ',', 'ĩ', 'Ả', 'D', 'G', 'ọ', 'Ở', 'Ư', '@', '.', 'Ĩ', 'ă', 'đ', 'h', 'ỏ', 'p', 'Ứ', '#', '|', 'ỵ', 'ắ', 'Đ', 'H', 'O', 'P', 'Ừ', '$', '\\', 'ý', 'ằ', 'e', 'i', 'Ó', 'q', 'Ự', '%', '`', 'ỳ', 'ặ', 'é', 'í', 'Ò', 'Q', 'Ử', '^', '~', 'ỷ', 'ẳ', 'è', 'ì', 'Ọ', 'r', 'v', '&', 'ã', 'ỹ', 'Ă', 'ẹ', 'ị', 'Ỏ', 'R', 'V', '*', 'Ã', 'Ỵ', 'Ắ', 'ẻ', 'ỉ', 'ô', 's', 'w', '(', 'ẵ', 'Ý', 'Ằ', 'E', 'I', 'ố', 'S', 'W', ')', 'Ẵ', 'Ỳ', 'Ặ', 'É', 'Í', 'ồ', 't', 'x', '-', 'ẫ', 'Ỷ', 'Ẳ', 'È', 'Ì', 'ộ', 'T', 'X', '_', 'Ẫ', 'Ỹ', 'â', 'Ẹ', 'Ị', 'ổ', 'u', 'y', '+', 'ẽ', 200, 'ấ', 'Ẻ', 'Ỉ', 'Ô', 'ú', 'Y', '=', 'Ẽ', 200, 'ầ', 'ê', 'j', 'Ố', 'ù', 'z', '\'', 'ễ', 200, 'ậ', 'ế', 'J', 'Ồ', 'ụ', 'Z', '\"', 'Ễ', 200, 'ẩ', 'ề', 'k', 'Ộ', 'ủ', '0', '?', 'ũ', 200, 'Â', 'ệ', 'K', 'Ổ', 'U', '1', '/', 'Ũ', 200
    };
    private final static int[] charWidth = {
        6, 7, 7, 3, 7, 7, 6, 2, 8, 6, 7, 6, 6, 7, 7, 6, 2, 8, 6, 7, 7, 9, 7, 7, 7, 4, 7, 6, 7, 6, 10, 7, 7, 6, 4, 7, 6, 7, 6, 7, 7, 8, 6, 3, 7, 7, 7, 7, 8, 7, 8, 6, 3, 7, 7, 5, 5, 7, 7, 8, 6, 7, 7, 7, 7, 6, 7, 7, 8, 6, 7, 7, 7, 7, 7, 7, 7, 8, 3, 3, 5, 7, 7, 7, 7, 7, 8, 15, 3, 5, 6, 8, 7, 7, 7, 8, 9, 2, 7, 6, 8, 7, 7, 7, 8, 6, 6, 6, 6, 6, 3, 7, 7, 8, 10, 3, 6, 6, 6, 3, 7, 7, 8, 6, 9, 6, 6, 6, 3, 7, 5, 6, 10, 6, 6, 7, 6, 3, 7, 7, 7, 6, 7, 7, 7, 6, 3, 7, 5, 9, 4, 7, 7, 7, 6, 3, 7, 7, 9, 4, 7, 7, 7, 6, 3, 7, 4, 6, 6, 6, 7, 7, 6, 3, 7, 7, 7, 7, 7, 7, 6, 6, 3, 7, 7, 6, 6, 6, 0, 7, 6, 3, 7, 7, 7, 7, 6, 0, 6, 6, 4, 7, 7, 6, 2, 6, 0, 6, 7, 5, 7, 7, 8, 4, 6, 0, 7, 6, 6, 7, 7, 6, 5, 7, 0, 7, 6, 7, 7, 7, 5, 6, 7, 0
    };
    //color
    ////////////////////////////////////////////////
    private static final int SUM_CHAR = arrChar.length;
    private static final int WIDTH_SPRITE = 17;
    private static final int HEIGHT_SPRITE = 17;
    private static final int SUM_SPRITEINROW = 9;
    private static final int LENG_RGBARR = WIDTH_SPRITE * HEIGHT_SPRITE;
    private static int rgbb[] = new int[LENG_RGBARR];
    public static int dyWH = 0;

    private static final void init() {
        loadFont("PipoFonts.pipo");
        Runtime.getRuntime().gc();
    }

    public static final int getLengString(byte[] b, int offset, int length) {
        return drawString(null, 0, 0, b, 0, 0, offset, length);
    }

    public static final int drawString(Graphics g, int x, int y, byte[] index, int color1, int color2, int offset, int length) {
        int lengStr = 0;
        int leng = offset + length;
        if (PiPoRGB == null) {
            init();
        }
        for (int i = offset; i < leng; i++) {
            if (index[i] != 127) {
                lengStr += drawRGB(g, index[i], x + lengStr, y - 2, color1, color2);
            } else {
                lengStr = 0;
                y += 17;
            }
        }
        return lengStr;
    }

    public static int drawString(Graphics g, int x, int y, String str, int color1, int color2, int offset, int length, int width) {
        byte[] b = new byte[str.length()];
        b = toByteIndex(str);
        return drawString(g, x, y, b, color1, color2, offset, length, width);
    }

    public static int drawString(Graphics g, int x, int y, byte[] index, int color1, int color2, int offset, int length, int width) {

        int lengStr = 0;
        int dem1 = 0, dem2 = -1;
        int leng = offset + length;
        dyWH = 0;
        if (PiPoRGB == null) {
            init();
        }

        for (int i = offset; i < leng; i++) {
            if ((index[i] == SUM_CHAR - 127) || (index[i] == 127)) {
                dem1 = dem2 + 1;
                dem2 = i - 1;

                int temp2 = getLengString(index, dem1, dem2 - dem1 + 1);
                if (temp2 + lengStr <= width) {
                    drawString(g, x + lengStr, y, index, color1, color2, dem1, dem2 - dem1 + 1);
                    if (index[i] == SUM_CHAR - 127) {
                        lengStr += temp2;
                    } else {
                        lengStr = 0;
                        dyWH = y;
                        y += 17;
                        dem2 += 1;
                    }
                } else {
                    dyWH = y;
                    y += 17;
                    dem1 += 1;
                    drawString(g, x, y, index, color1, color2, dem1, dem2 - dem1 + 1);
                    lengStr = temp2;
                }
            }
            //code here

        }
        if (dem2 != leng - 2) {
            dem1 = dem2 + 1;
            dem2 = leng - 1;
            int temp2 = getLengString(index, dem1, dem2 - dem1 + 1);
            if (temp2 + lengStr <= width) {
                drawString(g, x + lengStr, y, index, color1, color2, dem1, dem2 - dem1 + 1);
                lengStr = 0;
                dyWH = y;
                y += 17;
                dem2 += 1;
            } else {
                dyWH = y;
                y += 17;
                dem1 += 1;
                drawString(g, x, y, index, color1, color2, dem1, dem2 - dem1 + 1);
                lengStr = temp2;
            }
        }
        return lengStr;
    }

    public static final byte[] toByteIndex(String str) {
        int length = str.length();
        byte b[] = new byte[length];
        for (int i = 0; i < length; i++) {
            if (str.charAt(i) != '\n') {
                b[i] = (byte) (getIndex(str.charAt(i)) - 127);
            } else {
                b[i] = 127;
            }
        }
        return b;

    }

    private static void loadFont(String str) {
        InputStream is = PiPoDesigner.class.getResourceAsStream(str);
        try {
            PiPoRGB = new byte[is.available()];
            is.read(PiPoRGB);
            //   Log.d("size",""+ PiPoRGB.length);
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

    public static int drawCenterString(Graphics g, int x, int y, byte[] index, int color1, int color2, int offset, int length) {
        return drawString(g, x - getLengString(index, offset, length) / 2, y, index, color1, color2, offset, length);
    }

    private static int drawRGB(Graphics g, int index, int x, int y, int color1, int color2) {
        index += 127;
        int leng = 3;
        if (index != SUM_CHAR) {

            int temp = (index % SUM_SPRITEINROW) * WIDTH_SPRITE + (index / SUM_SPRITEINROW) * SUM_SPRITEINROW * LENG_RGBARR;
            if (g != null) {
                for (int i = 0; i < HEIGHT_SPRITE; i++) {
                    for (int j = 0; j < WIDTH_SPRITE; j++) {
                        switch (PiPoRGB[temp + j]) {
                            case 1:
                                rgbb[i * WIDTH_SPRITE + j] = color1;
                                break;
                            case 2:
                                rgbb[i * WIDTH_SPRITE + j] = color2;
                                break;
                            default:
                                rgbb[i * WIDTH_SPRITE + j] = 0;
                        }
                    }
                    temp += SUM_SPRITEINROW * WIDTH_SPRITE;
                }
            }
            leng = charWidth[index];
            if (g != null) {
                g.drawRGB(rgbb, 0, WIDTH_SPRITE, x, y, HEIGHT_SPRITE, WIDTH_SPRITE, true);
            }

        }
        return leng;
    }

    public static int getIndex(char ch) {
        int i;
        for (i = 0; i < SUM_CHAR; i++) {
            if (ch == arrChar[i]) {
                break;
            }
        }
        return i;
    }

    public static void drawCenterString(Graphics g, String str, int color1, int color2, int x, int y) {
        byte[] text = toByteIndex(str);
        drawCenterString(g, x, y, text, color1, color2, 0, str.length());
    }

    public static void drawString(Graphics g, String str, int color1, int color2, int x, int y) {
        byte[] text = toByteIndex(str);
        drawString(g, x, y, text, color1, color2, 0, str.length());
    }

//    public static int drawString(Graphics g, String str, int color1, int color2, int x, int y, int width, int height) {
//        byte[] text = toByteIndex(str);
//        return drawString(g, x, y, text, color1, color2, 0, str.length(), width);
//    }
    public static int drawString(Graphics g, String str, int offSet, int length, int color1, int color2, int x, int y) {
        byte[] text = toByteIndex(str);
        return drawString(g, x, y, text, color1, color2, offSet, length);
    }

    public static int stringWidth(String str) {
        
        byte[] text = toByteIndex(str);
        return getLengString(text, 0, str.length());
    }

    public static int stringWidth(String str, int offset, int length) {
        byte[] text = toByteIndex(str);
        return getLengString(text, offset, length);
    }
}
